package com.common.util;

import com.common.vo.TreeVo;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zyj
 * @description
 * @date 2018/7/31 14:11
 **/
@Component
public class TreeVoUtil {

    public TreeVo getTreeVo(List<TreeVo> listTreeVo, String parentId){
        TreeVo treeVo = null;
        if(null==parentId||parentId.isEmpty()){
            return treeVo;
        }

        int length = listTreeVo.size()-1;
        for(int i=length;i>=0;i--){
            if(listTreeVo.size()<=i){
                break;
            }
            TreeVo menuVoNext = listTreeVo.get(i);
            if(parentId.equals(menuVoNext.getId())){
                treeVo = menuVoNext;
                putValueVo(listTreeVo, menuVoNext, parentId);
            }
        }

        return treeVo;
    }

    public List<TreeVo> getListTreeVo(List<TreeVo> listTreeVo, String parentId){
        List<TreeVo> treeVos = new ArrayList<TreeVo>();
        if(null==parentId||parentId.isEmpty()){
            return treeVos;
        }

        for(int i=listTreeVo.size()-1;i>=0;i--){
            System.out.println(i);
            if(listTreeVo.size()<=i){
                break;
            }
            TreeVo treeVoNext = listTreeVo.get(i);
            if(parentId.equals(treeVoNext.getParentId())){
                putValueVo(listTreeVo, treeVoNext, treeVoNext.getId());
                treeVos.add(treeVoNext);
            }
        }
        return treeVos;
    }



    public void putValueVo(List<TreeVo> listTreeVo,TreeVo treeVo,String parentId){
        List<TreeVo> listTree = new ArrayList<TreeVo>();
        for(int i=listTreeVo.size()-1;i>=0;i--){
            TreeVo menuVo1 = listTreeVo.get(i);
            if(menuVo1.getParentId()!=null&&menuVo1.getParentId().equals(parentId)){
                listTree.add(menuVo1);
                listTreeVo.remove(i);
                putValueVo(listTreeVo, menuVo1, menuVo1.getId());
            }
        }
        treeVo.setTreeVoList(listTree);
    }

    /**
     * 根据父节点或取树
     * @param list
     * @param parsent
     * @return
     * @return: List<TreeVo>
     * @author zyj
     */
    public List<TreeVo> convertTreeVo(List<TreeVo> list, String parsent){
        List<TreeVo> treeList = new ArrayList<TreeVo>();

        if(list==null||list.isEmpty()){
            return treeList;
        }

        //循环遍历树查询的所有节点
        for (int i=(list.size()-1); i>=0 ; i--) {
            TreeVo node = list.get(i);
            if(this.isEmpty(node.getParentId())){
                list.remove(node);
                treeList.add(sortTree(list,node));//放置二级节点
            }else if(parsent!=null&&this.isEmpty(node.getParentId())){
                list.remove(node);
            }else if(parsent!=null&&node.getId().equals(parsent)){
                list.remove(node);
                treeList.add(sortTree(list,node));
            }
        }

        return treeList ;
    }

    private TreeVo sortTree(List<TreeVo> list, TreeVo rootVO) {

        if(this.isBlank(list)){
            return rootVO;
        }

        for(TreeVo vo : list){
            if(rootVO.getId().equals(vo.getParentId())){
                TreeVo treeVo = sortTree(list, vo);
                rootVO.getTreeVoList().add(treeVo);
            }
        }

        return rootVO;
    }

    /**
     * 判断string类型是否为空(空为true)
     * @param value
     * @return: boolean
     * @author zyj
     */
    public boolean isEmpty(String value){
        boolean b = true;
        if(value!=null&&!(value.trim()).equals("")){
            b = false;
        }

        return b;
    }

    /**
     * 判断List是否为空。 list==null或长度为0返回true,其它则返回false.
     *
     * @param list
     * @return
     */
    public boolean isBlank(List<? extends Object> list) {
        if (list == null || list.size() == 0) {
            return true;
        }
        return false;
    }
}
